package com.yy.controller.admin;

import com.yy.result.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * 店铺管理
 */
@RestController
@RequestMapping("/admin/shop")
@Api(tags = "店铺相关接口")
@Slf4j
public class ShopController {

    public static final String KEY = "SHOP_STATUS";

    @Autowired
    private RedisTemplate redisTemplate;

    @PutMapping("/{status}")          // 1. 补全斜杠
    public Result update(@PathVariable Integer status) {  // id 没用就删掉
        log.info("设置店铺的状态为：{}", status == 1 ? "营业中" : "打烊中");
        redisTemplate.opsForValue().set(KEY, status);     // 2. 用常量
        return Result.success();
    }

    @GetMapping("/status")
    public Result getStatus() {
        Integer status = (Integer) redisTemplate.opsForValue().get(KEY); // 用常量
        log.info("获取到店铺的状态为：{}", status == null ? "未设置" : (status == 1 ? "营业中" : "打烊中"));
        return Result.success(status == null ? 0 : status);   // 3. 防 NPE
    }

}

